﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Top.Web;
using Top.Web.Api;
using Top.PointStore.Business;
using Top.PointStore.LuckTurntable;
using Top.PointStore.Web.Filters;
using Top.Web.Api.Filters;

namespace Top.PointStore.Controllers
{
    [Provider]
    public class LuckProductController : ApiBase
    {
        //
        // GET: /T_LuckProduct/

        /// <summary>
        /// 获取中奖人数
        /// </summary>
        [Login]
        public object GetList()
        {
            List<LuckProduct> list = LuckBigWheel.GetLuckProductList();
            List<LuckProduct> orderbyList = list.OrderByDescending(a => a.Angle).ToList();
            if (orderbyList.Count==8)
            {
                LuckProduct angle0 = orderbyList.ElementAt(7);
                orderbyList.RemoveAt(7);
                orderbyList.Insert(0,angle0);
            }
            return orderbyList;             
        }

        public object GetLuckProduct(int ID)
        {
            List<LuckProduct> list = LuckBigWheel.GetLuckProductList();
            LuckProduct product = list.Where(a => a.ID == ID).FirstOrDefault();
            return product;
        }

        [Login]
        public async Task<object> Update()
        {
            string data = Post("data", "");
            SystemManager.AddLog(User.CrmUserID, "大转盘商品更改", "用户名：" + User.Name).Wait();
            return await LuckProductManager.Update(data);
        }

        public object Edit()
        {
            int id = Get("ID", 0);
            string productName = Get("ProductName", "");
            int productKid = Get("ProductKid", 0);
            int count = Get("Count", 0);
            double chance = Get("Chance", 0.00);
            double point = Get("Point", 0.00);
            string description = Get("Description", "");
            if (id<1)
            {
                throw new WebApiException("无效的奖品位置");
            }
            List<LuckProduct> list = LuckBigWheel.GetLuckProductList();
            double allChance = 0;
            bool haveThx = false;
            foreach (LuckProduct item in list)
            {
                if (item.ID!=id&&item.Kid==productKid)
                {
                    throw new WebApiException("同一商品不允许重复添加");
                }
                if (item.Kid==-1)
                {
                    haveThx = true;
                    continue;
                }
                if (item.ID==id)
                {
                    allChance = allChance + chance;
                    continue;
                }                
                allChance = allChance + item.Chance;
            }
            //if (allChance>100)
            //{
            //    throw new WebApiException("中奖概率超出100%");
            //}
            double thx =Convert.ToDouble((100.00 - allChance).ToString("0.00"));
            SystemManager.AddLog(User.CrmUserID, "大转盘商品更改", "用户名：" + User.Name).Wait();
            if (haveThx||productKid==-1)
            {
                return LuckProductManager.Edit(id, productName, productKid, count, chance, point, description, thx);
            }
            else
            {
                return LuckProductManager.Edit(id, productName, productKid, count, chance, point, description);
            }            
        }

        public object Delete()
        {
            int id = Get("ID", 0);
            if (id < 1)
            {
                throw new WebApiException("无效的奖品位置");
            }
            SystemManager.AddLog(User.CrmUserID, "大转盘商品删除", "用户名：" + User.Name).Wait();
            return LuckProductManager.Delete(id);
        }
        
        public object Clear()
        {
            SystemManager.AddLog(User.CrmUserID, "大转盘商品重置", "用户名：" + User.Name).Wait();
            //throw new WebApiException("活动进行中，禁止重置！！！");
            return LuckProductManager.Clear();
        }
    }
}
